package com.example.myapplication.tack_05;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.tack_05.data.SetPassWordData;
import com.example.myapplication.utils.OkHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

public class SetPassword extends AppCompatActivity {
    private ImageView exit;
    private EditText pwd;
    private EditText newsPwd;
    private LinearLayout save;
    private JSONObject jsonObject;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_05_set_password);
        init();
        exit_();
        callkSave();


    }

    private void callkSave() {
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String jsonPwd = pwd.getText().toString();
                String jsonNewsPwd = newsPwd.getText().toString();
                try {
                    jsonObject.put("oldPassword",jsonPwd);
                    jsonObject.put("newPassword",jsonNewsPwd);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String json = jsonObject.toString();
                        setPwd(json,jsonNewsPwd);
                    }
                });
            }
        });
    }

    private void setPwd( String json,String jsonNewsPwd) {
        String url = "prod-api/api/common/user/resetPwd";
        OkHttpTool.doNetRequest(url, OkHttpTool.PUT, SetPassWordData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                SetPassWordData setPassWordData = (SetPassWordData) obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        PwdAdapter(setPassWordData,jsonNewsPwd);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void PwdAdapter(SetPassWordData setPassWordData,String jsonNewsPwd) {
        if(setPassWordData.getCode() == 200){
            Toast.makeText(SetPassword.this,setPassWordData.getMsg(),Toast.LENGTH_SHORT).show();
            editor.putString("password",jsonNewsPwd);
            editor.apply();
            SetPassword.this.finish();
        }else {
            Toast.makeText(SetPassword.this,setPassWordData.getMsg(),Toast.LENGTH_SHORT).show();
        }
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetPassword.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        pwd = findViewById(R.id.pwd);
        save = findViewById(R.id.save);
        newsPwd = findViewById(R.id.news_pwd);
        jsonObject = new JSONObject();
        sharedPreferences = getSharedPreferences("login", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }
}